// pages/all-community/community.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    controls: false,
    islogin: false,
    shequ_img: '',
    shequlist: '',
    select: 0,
    imgvideo: [],
    fullScreenbtn: false,
    currentpage: 1,
    nogd:false,
    canjiazai:true
  },
  // 授权登录
  bindGetUserInfo(e) {
    var that = this;
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    } else {
      if (e.detail.userInfo) {
        wx.setStorageSync("nickName", e.detail.userInfo.nickName)
        wx.setStorageSync("avatarUrl", e.detail.userInfo.avatarUrl)
        console.log()
        // 用户按了允许授权按钮
        var code = wx.getStorageSync("code");
        ajax('/api/user/login', {
          data: {
            code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl,
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "put",
        }, res => {
          wx.setStorageSync("uqid", res.data.uqid)
          this.setData({
            islogin: true
          })
          this.getlist(1)
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
    }

  },
  // 0元拍/秒杀/鉴宝/论坛-顶部图/优势图
  getways: function() {
    ajax('/api/index/config_list', {
      data: {},
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        shequ_img: res.data.forum
      })
    })
  },

  play: function(e) {
    var index = e.currentTarget.dataset.fuindex
    var idx = e.currentTarget.dataset.index

    console.log(e)

    var videoContext = wx.createVideoContext('myvideo' + index + idx, this);
    videoContext.requestFullScreen();

    videoContext.play();

    var str = 'shequlist[' + index + '].plan_arrs[' + idx + '].fullScreen'
    this.setData({
      [str]: true
    })
  },



  /**关闭视屏 */
  closeVideo(e) {

    var index = e.currentTarget.dataset.fuindex
    var idx = e.currentTarget.dataset.index

    //执行退出全屏方法
    var videoContext = wx.createVideoContext('myvideo' + index + idx, this);
    videoContext.exitFullScreen();
    videoContext.pause();

  },
  /**视屏进入、退出全屏 */

  fullScreen(e) {

    var index = e.currentTarget.dataset.fuindex
    var idx = e.currentTarget.dataset.index

    //执行退出全屏方法
    var videoContext = wx.createVideoContext('myvideo' + index + idx, this);

    var isFull = e.detail.fullScreen;
    //视屏全屏时显示加载video，非全屏时，不显示加载video
    this.setData({
      fullScreen: isFull
    })

    console.log(isFull)

    if (isFull == false) {
      videoContext.pause();
    }

  },


  seebig: function(e) {
    var url = e.currentTarget.dataset.url
    console.log(e)
    var urls = []
    urls.push(url)
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    });
  },


  totop: function(e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },


  lower:function(){
   
    if(this.data.canjiazai){
      this.setData({
        canjiazai: false
      })
      if(this.data.nogd==false){
        var currentpage=this.data.currentpage
        console.log(currentpage)

        this.getlist(currentpage+1)

      }else{
        console.log("没有更多")
      }

    }else{
      console.log("不能加载")
    }
  },

  // 全部社区列表
  getlist: function(page) {
    ajax('/api/index/forum_list', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        page: page,
        limit: 20,
        type: this.data.select - 1 == -1 ? '' : this.data.select - 1,
        is_my: 0
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (page == 1) {
        if (res.data.list.length > 0) {
          var arr = res.data.list
          for (var a in arr) {
            if (arr[a].plan_arr) {
              arr[a].plan_arrs = []
            }
          }
          var li1 = '.jpg',
            li2 = '.jpeg',
            li3 = '.png',
            li4 = '.gif';
          for (var b in arr) {
            for (var c in arr[b].plan_arr) {
              if (arr[b].plan_arr[c].includes(li1) || arr[b].plan_arr[c].includes(li2) || arr[b].plan_arr[c].includes(li3) || arr[b].plan_arr[c].includes(li4)) {
                arr[b].plan_arrs[c] = {
                  'url': arr[b].plan_arr[c],
                  'style': '0'
                }
              } else {
                arr[b].plan_arrs[c] = {
                  'url': arr[b].plan_arr[c],
                  'style': '1',
                  'fullScreen': false
                }
              }
            }
          }

          console.log(res.data.list)

          this.setData({
            shequlist: res.data.list
          })
        }else{

          console.log(res.data.list.length)

          this.setData({
            nogd:true,
            shequlist: res.data.list
          })
        }
        if (res.data.list.length < 20 && res.data.list.length > 0){
          this.setData({
            nogd: true,
            
          })
        }

      } else {

        var shequlist = this.data.shequlist

        if (res.data.list.length > 0) {
          var arr = res.data.list
          for (var a in arr) {
            if (arr[a].plan_arr) {
              arr[a].plan_arrs = []
            }
          }
          var li1 = '.jpg',
            li2 = '.jpeg',
            li3 = '.png',
            li4 = '.gif';
          for (var b in arr) {
            for (var c in arr[b].plan_arr) {
              if (arr[b].plan_arr[c].includes(li1) || arr[b].plan_arr[c].includes(li2) || arr[b].plan_arr[c].includes(li3) || arr[b].plan_arr[c].includes(li4)) {
                arr[b].plan_arrs[c] = {
                  'url': arr[b].plan_arr[c],
                  'style': '0'
                }
              } else {
                arr[b].plan_arrs[c] = {
                  'url': arr[b].plan_arr[c],
                  'style': '1',
                  'fullScreen': false
                }
              }
            }
          }

          for (var x in arr){
            shequlist.push(arr[x])
          }
          this.setData({
            shequlist: shequlist,
            currentpage: this.data.currentpage+1,
            canjiazai:true,
          })
        } else {
          console.log(res.data.list.length)
          this.setData({
            nogd: true,
          })
        }
        if (res.data.list.length < 20 && res.data.list.length > 0) {
          this.setData({
            nogd: true,
          })
        }
      }
    })
  },





  // 点赞
  dianzan: function(e) {
    var id = e.currentTarget.dataset.id
    ajax('/api/user/love_add_or_del', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        type: 2,
        zid: id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "PUT",
    }, res => {
      this.getlist(1)
    })
  },
  // 选择
  active: function(e) {
    var idx = e.currentTarget.dataset.index;
    this.setData({
      select: idx
    })

    this.setData({
      currentpage: 1,
      nogd:false,
      shequlist:[],
      canjiazai:true
    })
    this.totop()
    this.getlist(1)

  },
  // 社区详情页面
  details: function(e) {
    var id = e.currentTarget.dataset.id;
    var zan = e.currentTarget.dataset.zan;
    wx.navigateTo({
      url: '/pages/community-details/details?id=' + id,
    })
  },
  // 评论
  pinglun: function(e) {
    var id = e.currentTarget.dataset.id;
    var zan = e.currentTarget.dataset.zan;
    wx.navigateTo({
      url: '/pages/community-details/details?id=' + id,
    })
  },
  // 我要发布
  fabu: function() {
    wx.navigateTo({
      url: '/pages/community-release/release',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getways()
    // this.getlist(1)
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    }


    this.setData({
      currentpage: 1,
      nogd: false,
      shequlist: [],
    })
    this.getlist(1)
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getlist(1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})